package com.firewolf.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 作者：刘兴
 * 时间:18/12/3
 **/
@RestController
@RequestMapping("user")
public class UserController {

    @PostMapping("login")
    public String login(String username, String password,boolean rememberMe) {
        System.out.println(username + "," + password);
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        Subject subject = SecurityUtils.getSubject();
        try {
            token.setRememberMe(rememberMe);
            subject.login(token);
        } catch (AuthenticationException e) {
           return  e.getMessage();
        }
        return "login success";
    }

    @GetMapping("list")
    @RequiresPermissions("user:list")
    public String list() {
//        Subject subject = SecurityUtils.getSubject();
//        subject.checkPermission("user:list");
        return "list";
    }


    @GetMapping("test")
    public String test() {
        Subject subject = SecurityUtils.getSubject();
        subject.checkPermission("user:list");
        return "test";
    }

    @RequestMapping("logout")
    public String logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "logout";
    }

}
